package com.ruoyi.communication.mapper;

import java.util.List;

import com.ruoyi.communication.domain.IpControl;
import org.apache.ibatis.annotations.*;

/**
 * @description: TODO
 * @author dai
 * @date 2022/12/14 17:37
 * @version 1.0
 */

public interface IpControlMapper {

    @Results(id = "ipControlMap", value = {
            @Result(column = "ip", property = "ip", javaType = String.class),
            @Result(column = "port", property = "port", javaType = Integer.class),
            @Result(column = "ip_type", property = "ipType", javaType = Integer.class),
            @Result(column = "device_data_gather_cycle", property = "deviceDataGatherCycle", javaType = Integer.class),
            @Result(column = "device_data_gather_interval", property = "deviceDataGatherInterval", javaType = Integer.class),
            @Result(column = "note_alarm_data_gather_cycle", property = "noteAlarmDataGatherCycle", javaType = Integer.class),
            @Result(column = "environment_data_gather_cycle", property = "environmentDataRecordCycle", javaType = Integer.class)
    })
    //通过ip查询控制器
    @Select("select ip, port, ip_type, device_data_gather_cycle, device_data_gather_interval, note_alarm_data_gather_cycle, environment_data_gather_cycle " +
            "from control " +
            "where ip = #{ip}")
    IpControl queryControlByIp(String ip);

    //通过ip类型（0：区域控制器， 1：八防控制器）查询控制器
    @Select("select ip " +
            "from control " +
            "where ip_type = #{ipType}")
    List<String> queryAllIpControlIp(String ipType);

    //插入控制器
    @Insert("insert into control(ip, port, ip_type, device_data_gather_cycle, device_data_gather_interval, note_alarm_data_gather_cycle, environment_data_gather_cycle) " +
            "values (#{ip}, #{port}, #{ipType}, #{deviceDataGatherCycle}, #{deviceDataGatherInterval}, #{noteAlarmDataGatherCycle}, #{environmentDataRecordCycle})")
    void addIpControl(IpControl ipControl);

    //更新区域控制器
    @Update("<script>" +
            "update control " +
            "<set>" +
            "<if test='port !=null'>port = #{port},</if>" +
            "<if test='ipType !=null'>ip_type = #{ipType},</if>" +
            "<if test='pollInterval !=null'>device_data_gather_cycle  = #{deviceDataGatherCycle}, </if>" +
            "<if test='deviceInterval !=null'>device_data_gather_interval = #{deviceDataGatherInterval}, </if>" +
            "<if test='deviceInterval !=null'>note_alarm_data_gather_cycle = #{noteAlarmDataGatherCycle}, </if>" +
            "<if test='enviInterval !=null'>environment_data_gather_cycle = #{environmentDataRecordCycle}, </if>" +
            "</set> " +
            "where ip = #{ip}" +
            "</script>")
    void updateIpControl(IpControl ipControl);

    //通过ip删除相应的控制器
    @Delete("delete " +
            "from control " +
            "where ip = #{ip}")
    void deleteIpControl(String ip);
}

